package jzqb.controller.admin;

import haha.life.common.CutPageBean;
import haha.life.common.PermissionTypeTool;
import haha.life.common.SysTool;
import haha.life.normal.TokenUtils;
import io.swagger.annotations.Api;
import jzqb.annotation.PermissionCheck.PermissionCheck;
import jzqb.bo.DynamicNewsBo;
import jzqb.common.Result;
import jzqb.po.DynamicNewsPo;
import jzqb.service.IDynamicNewsService;
import jzqb.vo.DynamicNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/4/7 16:14
 * @Version 1.0
 */

@Api(tags = "动态新闻接口")
@RestController
@RequestMapping("/admin/dynamicNews")
public class AdminDynamicNewsController {

    @Autowired
    private IDynamicNewsService dynamicNewsService;

    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @GetMapping("/queryDynamicNews")
    public Result queryDynamicNews(@RequestParam("page") Integer page,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam(value = "startTime",required = false) String startTime,
                                   @RequestParam(value = "endTime",required = false) String endTime,
                                   @RequestParam(value = "searchTitle",required = false) String searchTitle,
                                   @RequestParam(value = "searchText",required = false) String searchText
    ) {
        DynamicNewsBo dynamicNewsBo=new  DynamicNewsBo();
        dynamicNewsBo.setPage(page);
        dynamicNewsBo.setPageSize(pageSize);
        dynamicNewsBo.setStart(page,pageSize);
        dynamicNewsBo.setStartTime(SysTool.dealTime(startTime, SysTool.left));
        dynamicNewsBo.setEndTime(SysTool.dealTime(endTime, SysTool.right));
        dynamicNewsBo.setTitle(searchTitle);
        dynamicNewsBo.setText(searchText);
        CutPageBean<DynamicNewsVo> cutPageBean = dynamicNewsService.queryDynamicNews(dynamicNewsBo);
        return Result.succ(cutPageBean);
    }

    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping("/addDynamicNews")
    public Result addDynamicNews(@RequestBody DynamicNewsPo dynamicNewsPo, HttpServletRequest request) throws UnsupportedEncodingException {
        dynamicNewsPo.setCreatTime(SysTool.getCurrentTime(SysTool.dateFormat_milli));
        dynamicNewsPo.setEntryPerson(TokenUtils.getCurrentUserId(request));
        Boolean bl = dynamicNewsService.addDynamicNews(dynamicNewsPo);
        if(bl){
            return   Result.succ("添加成功！");
        }else{
            return   Result.succ("添加失败");
        }
    }

    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping("/updateDynamicNews")
    public Result updateDynamicNews(@RequestBody DynamicNewsPo dynamicNewsPo, HttpServletRequest request) throws UnsupportedEncodingException {
        dynamicNewsPo.setUpdateTime(SysTool.getCurrentTime(SysTool.dateFormat_milli));
        Boolean bl =  dynamicNewsService.updateDynamicNews(dynamicNewsPo);

        if(bl){
            return   Result.succ("编辑成功!");
        }else{
            return   Result.succ("编辑失败!");
        }
    }

    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping("/delDynamicNews")
    public Result delDynamicNews(@RequestBody DynamicNewsPo dynamicNewsPo){
        Boolean bl =  dynamicNewsService.delDynamicNews(dynamicNewsPo.getId());
        if(bl){
            return   Result.succ("删除成功！");
        }else{
            return   Result.succ("删除失败!");
        }
    }




}
